package com.mlz.mlzmall.ui.sql;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据库操作封装类
 * Created by xbin on 2015/9/1.
 */
public class UserHelper {
    private Context context;
    private DatabaseHelper dh;
    private SQLiteDatabase db;
    // 定义用户表插入语句
    String delStr="delete from db_user where userID=?";
    String upStr="update db_user set user=?,password=? where userID=?";
    String addStr="insert into db_user(userID,user,password,userType,phone,idCard,email) values(?,?,?,?,?,?,?)";
    // 定义用户表查找语句
    String queryUser = "query into db_user(user,password,userType) values(?,?,?)";


    public UserHelper(Context context) {
        this.context = context;
        dh = new DatabaseHelper(context);
    }

    //注册user password userType userID idCard
    public boolean addData(String userID, String user, String password, String userType, String tel, String idCard, String email) {
        db = dh.getWritableDatabase();
        db.execSQL(addStr, new Object[]{userID,user, password, userType,tel,idCard,email});
        db.close();
        return true;
    }

    //登录用
    public boolean login(String username, String password) {
        db = dh.getReadableDatabase();
        String sql = "select * from db_user where user=? and password=?";
        Cursor cursor = db.rawQuery(sql, new String[]{username, password});
        if (cursor.moveToFirst() == true) {
            cursor.close();
            return true;
        }
        return false;
    }

    /***********************************************************/
    /**
     * 添加一条数据
     * @param user
     */

    public void save(UserInfo user){
        db=dh.getWritableDatabase();
        db.execSQL(addStr, new Object[]{user.getUserID(),user.getUser(),user.getPassWord(),user.getUserType(),user.getTel(),user.getIdCard(),user.getEmail()});
        db.close();
    }
    /**
     * 删除一条数据
     * @param uid
     */

    public void delete(String uid){
        db=dh.getWritableDatabase();
        db.execSQL(delStr, new Object[]{uid});
        db.close();
    }
    /**
     * 更新一条数据
     * @param user
     */
    public void update(UserInfo user){
        db=dh.getWritableDatabase();
        db.execSQL(upStr, new Object[]{user.getUser(),user.getPassWord()});
        db.close();
    }
    /**
     * 查找一条数据
     * @param uid
     */
    public UserInfo find(String uid){
        db=dh.getReadableDatabase();
        Cursor cursor =db.rawQuery("select * from db_user where userID=?", new String[]{uid.toString()});
        if(cursor.moveToFirst()){
            String uid2=cursor.getString(cursor.getColumnIndex("userID"));
            String user=cursor.getString(cursor.getColumnIndex("user"));
            String password=cursor.getString(cursor.getColumnIndex("password"));
            UserInfo userInfo=new UserInfo();
            userInfo.setUserID(uid2);
            userInfo.setUser(user);
            userInfo.setPassWord(password);
            return userInfo;
        }
        cursor.close();
        return null;
    }
    /**
     * 分页查找数据
     * @param offset 跳过多少条数据
     * @param maxResult 每页多少条数据
     * @return
     */
    public List<UserInfo> getScrollData(int offset, int maxResult){
        List<UserInfo> users=new ArrayList<UserInfo>();
        db=dh.getReadableDatabase();
        Cursor cursor =db.rawQuery("select * from db_user order by uid asc limit ?,?", new String[]{String.valueOf(offset), String.valueOf(maxResult)});
        while(cursor.moveToNext()){
            String uid2=cursor.getString(cursor.getColumnIndex("userID"));
            String user=cursor.getString(cursor.getColumnIndex("user"));
            String password=cursor.getString(cursor.getColumnIndex("password"));
            UserInfo userInfo=new UserInfo();
            userInfo.setUserID(uid2);
            userInfo.setUser(user);
            userInfo.setPassWord(password);
            users.add(userInfo);
        }
        return users;
    }
    /**
     * 获取数据总数
     * @return
     */
    public long getCount(){
        db=dh.getReadableDatabase();
        Cursor cursor =db.rawQuery("select count(*) from db_user", null);
        cursor.moveToFirst();
        long reslut=cursor.getLong(0);
        return reslut;
    }
}
